package com.inspur.cloud.controller.resource;

import com.inspur.cloud.entity.dataspace.ao.resource.DistributeAO;
import com.inspur.cloud.entity.dataspace.common.OpEntityResult;
import com.inspur.cloud.entity.dataspace.common.PageReqParam;
import com.inspur.cloud.entity.dataspace.common.PageResult;
import com.inspur.cloud.entity.dataspace.vo.HbaseAddVO;
import com.inspur.cloud.entity.dataspace.vo.HbaseReq;
import com.inspur.cloud.entity.dataspace.vo.HbaseVO;
import com.inspur.cloud.security.AuthorizationHelper;
import com.inspur.cloud.service.dataspace.HbaseManagerService;
import com.inspur.cloud.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.security.RolesAllowed;

@RestController
@RequestMapping("/dataspace/api/v1/hbase")
@Api(value = "hbase", description = "hbase资源管理")
public class HbaseManagerController {
    @Autowired
    HbaseManagerService hbaseManagerService;

    @RequestMapping(value = "/pubList", method = {RequestMethod.POST})
    @ApiOperation(value = "hbase列表查询", notes = "")
    public PageResult<HbaseVO> pubList(@ApiParam(value = "hbase查询对象,json格式请求")
                                       @RequestBody PageReqParam<HbaseReq> hbaseReq) {
        PageResult<HbaseVO> result = new PageResult<>(1, "查询成功");
        if (hbaseReq.getReqParam() == null || hbaseReq.getReqParam().getType() == null) {
            result.setCode(-1);
            result.setMsg("参数不完整/缺少type参数");
            return result;
        }
        if (hbaseReq.getPageSize() > 50 || hbaseReq.getPageSize() < 0) {
            result.setCode(-1);
            result.setMsg("页码非法");
            return result;
        }
        if (hbaseReq.getReqParam().getResourceId() == null) {
            hbaseReq.getReqParam().setResourceId(0);//设置一个默认ID
        }
        String resourceName=hbaseReq.getReqParam().getResourceName();
        if(!StringUtil.isEmpty(resourceName)){//过滤特殊字符，解决搜索问题
            resourceName=StringUtil.specialStr(resourceName);
            hbaseReq.getReqParam().setResourceName(resourceName);
        }
        if (AuthorizationHelper.isAdmin()) {//预留，考虑后期不好处理的时候扩展
            result = hbaseManagerService.showForAdmin(hbaseReq);
        } else {
            result = hbaseManagerService.showForUser(hbaseReq);
        }
        return result;
    }

    @PostMapping("/addRes")
    @ApiOperation(value = "新建资源", notes = "仅支持创建命名空间、表名、列族名")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> addResource(@ApiParam(value = "资源对象")
                                               @RequestBody HbaseAddVO hbaseVO
    ) throws Exception {
        if(hbaseVO.getType()==null){
            return OpEntityResult.fail("type参数不能为空");
        }
        if (hbaseVO.getParentId() == null) {
            hbaseVO.setParentId(0);//保证进入service后parentId不为null
        }
        if (AuthorizationHelper.isAdmin()) {
            if (StringUtil.isEmpty(hbaseVO.getSpaceName())) {
                return OpEntityResult.fail("命名空间参数不能为空");
            }
        } else {
            if (StringUtil.isEmpty(hbaseVO.getTableName()) && StringUtil.isEmpty(hbaseVO.getColumnFamilyName())) {
                return OpEntityResult.fail("表名/列族名参数不能为空");
            }
        }
        OpEntityResult<Integer> result = hbaseManagerService.addResource(hbaseVO);
        return result;
    }

    @PostMapping("/distributeRes")
    @ApiOperation(value = "分配资源", notes = "")
    public OpEntityResult<Integer> distributeTenant(@ApiParam(value = "资源对象")
                                                    @RequestBody DistributeAO hbaseDistributeAO) throws Exception {
        if (hbaseDistributeAO.getResourceId() == null) {
            return OpEntityResult.fail("资源ID不可为空");
        }
        OpEntityResult<Integer> result = hbaseManagerService.disSpace(hbaseDistributeAO, true);
        return result;
    }

    @PostMapping("/shareRes")
    @ApiOperation(value = "分享资源", notes = "")
    public OpEntityResult<Integer> shareRes(@ApiParam(value = "资源对象")
                                            @RequestBody DistributeAO hbaseDistributeAO) throws Exception {
        if (hbaseDistributeAO.getResourceId() == null) {
            return OpEntityResult.fail("资源ID不可为空");
        }
        OpEntityResult<Integer> result = hbaseManagerService.share(hbaseDistributeAO);
        return result;
    }

    @DeleteMapping("/delSpace")
    @ApiOperation(value = "资源删除", notes = "hbase目前未实现保留数据删除的功能")
    @RolesAllowed({"Admin", "Tenant"})
    public OpEntityResult<Integer> delSpace(@ApiParam(value = "资源id", example = "1")
                                            @RequestParam(value = "resourceId", required = true) Integer resourceId,
                                            @ApiParam(value = "资源名完整路径")
                                            @RequestParam(value = "resourceName",required = false) String resourceName,
                                            @ApiParam(value = "是否保留数据，1保留0不保留")
                                            @RequestParam(value = "keepData", required = true,defaultValue = "0") Integer keepData) {
        OpEntityResult<Integer> result = new OpEntityResult();
        if (resourceId == null) {
            result.setCode(-1);
            result.setMsg("资源id不可为空");
            return result;
        }
        return hbaseManagerService.delRes(resourceId, resourceName, keepData);
    }


}
